package cn.newzai.parser.proto.result

import scala.collection.mutable

import generate._
import cn.newzai.util.WriteFile

/**
 * Created with IntelliJ IDEA.
 * User: chenxiaohong
 * Date: 13-6-4
 * Time: 上午9:37
 * To change this template use File | Settings | File Templates.
 */
abstract class Enum(val name :String,
            val values :Array[(String,Int)]) {

  val default = values(0)._1

  val indexes  = for( i <- 0 until values.length) yield { (values(i)._1,i)}

  override def toString: String = s" enum ${name}"

  def generate_code( gen_type : GenerateCodeType.Value) :String
  def generate_binding( path : String, row : Int) = {
      def eachItem( itemName : String ) =
      {
        s"  <ComboBoxItem>${itemName}</ComboBoxItem>"
      }

      s"""
        |<ComboBox Grid.Column="1" Grid.Row="${row}"  SelectedIndex="{Binding Path=${path},Converter={StaticResource ${name.toLowerCase}2Index },Mode=TwoWay}">
        |${values.map( v => eachItem(v._1)).mkString("\n")}
        |</ComboBox>
      """.stripMargin

  }
}

object Enum{

  val enums = new mutable.HashMap[String,Enum]()
   def apply( name :String , values :Array[(String,Int)]) ={

     val e = new Enum(name,values)
       with   GenerateCSharpConvert2Index
       with   GenerateCSharpConvert2String
       with   GenerateCSharpEnumResource
       with   GenerateCSharpEnumCombox
       with GenerateCSharpEnumRadioGroup
     enums += ((e.name,e))
     e
   }

  def isEnum( name :String) = enums.contains(name)
  def getEnum(name :String) = {
    if( isEnum(name))
    {
      enums(name)
    }else{
      println(s"key for ${name} not exist in enum.")
      throw  new NoSuchElementException(s"key for ${name} not exist in enum.")
    }
  }

  def generate_convert() =
  {
    s"""
      |using System;
      |using System.Net;
      |using System.Windows;
      |using System.Windows.Controls;
      |using System.Windows.Documents;
      |using System.Windows.Ink;
      |using System.Windows.Input;
      |using System.Windows.Media;
      |using System.Windows.Media.Animation;
      |using System.Windows.Shapes;
      |using System.Windows.Data;
      |
      |namespace ${cn.newzai.Main.csharp_namespace}
      |{
      | ${enums.map( _._2.generate_code(GenerateCodeType.CSharpEnumValue2Index)).mkString("\n")}
      | ${enums.map( _._2.generate_code(GenerateCodeType.CSharpEnumValue2String)).mkString("\n")}
      |}
    """.stripMargin
  }

  def generate_resource() = {
    enums.map( _._2.generate_code(GenerateCodeType.CSharpEnumResource)).mkString("\n")
  }

  def generate_combox( dir : String)
  {
    for( e <- enums)
    {
      val fileName = s"${dir}\\${e._1}.combox.xml"
      WriteFile.write(fileName, e._2.generate_code(GenerateCodeType.CSharpEnumCombox))
    }
  }

  def generate_radio(dir :String){
    for( e <- enums)
    {
      val fileName = s"${dir}\\${e._1}.radio.xml"
      WriteFile.write(fileName, e._2.generate_code(GenerateCodeType.CSharpEnumRadioGroup))
    }
  }

}

trait EnumGenerate
{
  this : Enum =>

  def generate_code( gen_type : GenerateCodeType.Value):String =  s"/*Nothing to do for ${gen_type}*/"
}